package com.xuxueli.commontdemo.util;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.xuxueli.commontdemo.entity.DateDays;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.*;
import java.util.*;

/**
 * 日期工具
 */
@Slf4j
public class DateUtils {

    private static String pattern = "yyyy-MM-dd HH:mm:ss";
    private static String datePattern = "yyyy-MM-dd";

    public static void main(String[] args) {
        String beforeDay = beforeDay();
        System.out.println("获取昨天的日期："+beforeDay);
        Long str2TimeStamp = str2TimeStamp(getTime(), "yyyy-MM-dd HH:mm:ss");
        System.out.println("获取当前时间戳："+str2TimeStamp);
        Long daySub = getDaySub(new Date(), new Date());
        System.out.println("两个时间之间的天数："+daySub);
        long daySub1 = getDaySub("2022-11-11", "2023-11-29");
        System.out.println("两个时间之间的天数："+daySub1);
        Date currentDate = new Date();
        Calendar midnight=Calendar.getInstance();
        midnight.setTime(currentDate);
        midnight.add(midnight.DAY_OF_MONTH,1);//将日加1
        midnight.set(midnight.HOUR_OF_DAY,0);//控制时
        midnight.set(midnight.MINUTE,0);//控制分
        midnight.set(midnight.SECOND,0);//控制秒
        midnight.set(midnight.MILLISECOND,0);//毫秒
        System.out.println("获取明天开始时间："+DateUtils.formatDate(midnight.getTime(),"yyyy-MM-dd HH:mm:ss"));
        Integer remainSecondsOneDay = getRemainSecondsOneDay(currentDate);
        Integer remainSecondsOneDays = getRemainSecondsOneDays(currentDate);
        System.out.println("Calendar获取当天剩余的秒："+remainSecondsOneDay);
        System.out.println("LocalDateTime获取当天剩余的秒："+remainSecondsOneDays);
        LocalDateTime expireTime = LocalDateTime.now().withHour(23).withMinute(59).withSecond(59);
        Date date = Date.from(expireTime.atZone(ZoneId.systemDefault()).toInstant());
        System.out.println("当前结束时间："+DateUtils.formatDate(date,"yyyy-MM-dd HH:mm:ss"));
        long dayEndSecond = getDayEndSecond(new Date());
        System.out.println("时间距离当天结束时间的秒："+dayEndSecond);
        getCurrentDate();
        LocalDateTime localDateTime = LocalDateTime.now();
        LocalDate localDate = LocalDate.now();
        Date dateByLocalDateTime = asDateByLocalDateTime(localDateTime);
        System.out.println("DateByLocalDateTime："+DateUtils.formatDate(dateByLocalDateTime,"yyyy-MM-dd HH:mm:ss"));
        Date dateByLocalDate = asDateByLocalDate(localDate);
        System.out.println("DateByLocalDate："+DateUtils.formatDate(dateByLocalDate,"yyyy-MM-dd HH:mm:ss"));
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime localDateTime1 = asLocalDateTime(dateByLocalDateTime);
        LocalDate localDate1 = asLocalDate(dateByLocalDate);
        System.out.println("LocalDateTimeByDate："+dateTimeFormatter.format(localDateTime1));
        System.out.println("LocalDateByDate："+localDate1);
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDate today = LocalDate.now(); // 今天的日期
        LocalDate todayOfLastWeek  = today.minusWeeks(1);   // 上周的今天
        LocalDate todayOfLastMonth  = today.minusMonths(1); // 上个月的今天
        LocalDate todayOfLastYear  = today.minusYears(1);   // 去年的今天
        LocalDate todayOfNextWeek  = today.plusWeeks(1);    // 下周的今天
        LocalDate todayOfNextMonth  = today.plusMonths(1);  // 下个月的今天
        LocalDate todayOfNextYear  = today.plusYears(1);    // 明年的今天
        System.out.println("今年的第一天：" + today.with(TemporalAdjusters.firstDayOfYear()).toString());
        System.out.println("今年的最后一天：" + today.with(TemporalAdjusters.lastDayOfYear()).toString());
        System.out.println("本月的第一天：" + today.with(TemporalAdjusters.firstDayOfMonth()).toString());
        System.out.println("本月的最后一天：" + today.with(TemporalAdjusters.lastDayOfMonth()).toString());
        System.out.println("本周第一天："+getOneDayOfWeek(today, 1));
        System.out.println("本周最后一天："+getOneDayOfWeek(today, 7));
        System.out.println("***********************************");
        System.out.println("去年的第一天：" + todayOfLastYear.with(TemporalAdjusters.firstDayOfYear()).toString());
        System.out.println("去年的最后一天：" + todayOfLastYear.with(TemporalAdjusters.lastDayOfYear()).toString());
        System.out.println("上个月的第一天：" + todayOfLastMonth.with(TemporalAdjusters.firstDayOfMonth()).toString());
        System.out.println("上个月的最后一天：" + todayOfLastMonth.with(TemporalAdjusters.lastDayOfMonth()).toString());
        System.out.println("上周第一天："+getOneDayOfWeek(todayOfLastWeek,1));
        System.out.println("上周最后一天："+getOneDayOfWeek(todayOfLastWeek,7));
        System.out.println("***********************************");
        System.out.println("明年的第一天：" + todayOfNextYear.with(TemporalAdjusters.firstDayOfYear()).toString());
        System.out.println("明年的最后一天：" + todayOfNextYear.with(TemporalAdjusters.lastDayOfYear()).toString());
        System.out.println("下个月的第一天：" + todayOfNextMonth.with(TemporalAdjusters.firstDayOfMonth()).toString());
        System.out.println("下个月的最后一天：" + todayOfNextMonth.with(TemporalAdjusters.lastDayOfMonth()).toString());
        System.out.println("下周第一天："+ getOneDayOfWeek(todayOfNextWeek,1));
        System.out.println("下周最后一天："+ getOneDayOfWeek(todayOfNextWeek,7));
        System.out.println("***********************************");
        System.out.println("当天的开始时间：" + dtf.format(LocalDateTime.of(today, LocalTime.MIN)));
        System.out.println("当天的结束时间：" + dtf.format(LocalDateTime.of(today, LocalTime.MAX)));
        System.out.println("=========================" + LocalDateTime.now() + " 之前 ==================================");
        System.out.println("10秒前 ==> " + subtract(LocalDateTime.now(), 10, ChronoUnit.SECONDS));
        System.out.println("10分前 ==> " + subtract(LocalDateTime.now(), 10, ChronoUnit.MINUTES));
        System.out.println("一小时前 ==> " + subtract(LocalDateTime.now(), 1, ChronoUnit.HOURS));
        System.out.println("半天前 ==> " + subtract(LocalDateTime.now(), 1, ChronoUnit.HALF_DAYS));
        System.out.println("一天前 ==> " + subtract(LocalDateTime.now(), 1, ChronoUnit.DAYS));
        System.out.println("一月前 ==> " + subtract(LocalDateTime.now(), 1, ChronoUnit.MONTHS));
        System.out.println("一年前 ==> " + subtract(LocalDateTime.now(), 1, ChronoUnit.YEARS));
        System.out.println("==========================" + LocalDateTime.now() + " 之后 ===================================");
        System.out.println("10秒后 ==> " + plus(LocalDateTime.now(), 10, ChronoUnit.SECONDS));
        System.out.println("10分后 ==> " + plus(LocalDateTime.now(), 10, ChronoUnit.MINUTES));
        System.out.println("一小时后 ==> " + plus(LocalDateTime.now(), 1, ChronoUnit.HOURS));
        System.out.println("半天后 ==> " + plus(LocalDateTime.now(), 1, ChronoUnit.HALF_DAYS));
        System.out.println("一天后 ==> " + plus(LocalDateTime.now(), 1, ChronoUnit.DAYS));
        System.out.println("一月后 ==> " + plus(LocalDateTime.now(), 1, ChronoUnit.MONTHS));
        System.out.println("一年后 ==> " + plus(LocalDateTime.now(), 1, ChronoUnit.YEARS));
        System.out.println("================================= 时间差 =====================================");
        //  输出 ====>
        LocalDateTime start = LocalDateTime.of(2019, 10, 13, 11, 11);
        LocalDateTime end = LocalDateTime.of(2020, 11, 13, 13, 13);
        System.out.println("输出:" + formatTime(start, "yyyy-MM-dd HH:mm:ss") + " -到- " + formatTime(end, "yyyy-MM-dd HH:mm:ss") + "的时间差");                                     //====> 年:1
        System.out.println("年:" + betweenTwoTime(start, end, ChronoUnit.YEARS));      // ====> 年:1
        System.out.println("月:" + betweenTwoTime(start, end, ChronoUnit.MONTHS));     // ====> 月:13
        System.out.println("日:" + betweenTwoTime(start, end, ChronoUnit.DAYS));       // ====> 日:396
        System.out.println("半日:" + betweenTwoTime(start, end, ChronoUnit.HALF_DAYS));// ====> 半日:792
        System.out.println("小时:" + betweenTwoTime(start, end, ChronoUnit.HOURS));    // ====> 小时:9506
        System.out.println("分钟:" + betweenTwoTime(start, end, ChronoUnit.MINUTES));  // ====> 分钟:570362
        System.out.println("秒:" + betweenTwoTime(start, end, ChronoUnit.SECONDS));    // ====> 秒:34221720
        System.out.println("毫秒:" + betweenTwoTime(start, end, ChronoUnit.MILLIS));   // ====> 毫秒:34221720000
        System.out.println("================================== 时间大小 ===================================");
        System.out.println("2019-10-13 11:11:00 < 2020-11-13 13:13:00 = " + isBefore(start, end)); //t1 < t2 = true
        System.out.println("2019-10-13 11:11:00 > 2020-11-13 13:13:00 = " + isAfter(start, end));  //t1 > t2 = true

        System.out.println("================================== 周几 ===================================");
        LocalDateTime of1 = LocalDateTime.of(2020, 4, 20, 0, 0);
        LocalDateTime of2 = LocalDateTime.of(2020, 4, 21, 0, 0);
        LocalDateTime of3 = LocalDateTime.of(2020, 4, 22, 0, 0);
        LocalDateTime of4 = LocalDateTime.of(2020, 4, 23, 0, 0);
        LocalDateTime of5 = LocalDateTime.of(2020, 4, 24, 0, 0);
        LocalDateTime of6 = LocalDateTime.of(2020, 4, 25, 0, 0);
        LocalDateTime of7 = LocalDateTime.of(2020, 4, 26, 0, 0);
        System.out.println(formatTime(of1, "yyyy-MM-dd") + " 是周 " + week(of1));
        System.out.println(formatTime(of2, "yyyy-MM-dd") + " 是周 " + week(of2));
        System.out.println(formatTime(of3, "yyyy-MM-dd") + " 是周 " + week(of3));
        System.out.println(formatTime(of4, "yyyy-MM-dd") + " 是周 " + week(of4));
        System.out.println(formatTime(of5, "yyyy-MM-dd") + " 是周 " + week(of5));
        System.out.println(formatTime(of6, "yyyy-MM-dd") + " 是周 " + week(of6));
        System.out.println(formatTime(of7, "yyyy-MM-dd") + " 是周 " + week(of7));
        System.out.println("=========================== 周开头和结束 ===============================");
        System.out.println(" 上周的周一是 " + weekFirst(-1));
        System.out.println(" 上周的周末是 " + weekLast(-1));
        System.out.println(" 本周的周一是 " + weekFirst(0));
        System.out.println(" 本周的周末是 " + weekLast(0));
        System.out.println(" 下周的周一是 " + weekFirst(1));
        System.out.println(" 下周的周末是 " + weekLast(1));
        System.out.println("=========================== 月开头和结束 ===============================");
        System.out.println(" 上月的月初是 " + monthFirst(-1));
        System.out.println(" 上月的月位是 " + monthLast(-1));
        System.out.println(" 本月的月初是 " + monthFirst(0));
        System.out.println(" 本月的月位是 " + monthLast(0));
        System.out.println(" 下月的月初是 " + monthFirst(1));
        System.out.println(" 下月的月位是 " + monthLast(1));

    }

    public static String formatByMill(long timeMill, String pattern) {
        Date date = new Date(timeMill);
        return format(date, pattern);
    }

    public static String dateToString(Date date) {
        return dateToString(date, pattern);
    }

    public static String dateToString(Date date, String pattern) {
        SimpleDateFormat var2 = new SimpleDateFormat(pattern);
        return var2.format(date);
    }

    public static Date stringToDate(String strDate) {
        return stringToDate(strDate, pattern);
    }

    public static Date stringToDate(String strDate, String pattern) {
        SimpleDateFormat var2 = new SimpleDateFormat(pattern);

        try {
            return var2.parse(strDate);
        } catch (ParseException var4) {
            log.error("解析事件错误:" + strDate, var4);
            return null;
        }
    }

    public static java.sql.Date stringToSqlDate(String strDate) {
        return stringToSqlDate(strDate, datePattern);
    }

    public static java.sql.Date stringToSqlDate(String strDate, String pattern) {
        SimpleDateFormat var2 = new SimpleDateFormat(pattern);

        try {
            Date var3 = var2.parse(strDate);
            return new java.sql.Date(var3.getTime());
        } catch (ParseException var4) {
            log.error("解析事件错误:" + strDate, var4);
            return null;
        }
    }

    public static String dateToString(java.sql.Date date) {
        return dateToString(new Date(date.getTime()), datePattern);
    }

    public static String dateToString(Timestamp date) {
        return dateToString(new Date(date.getTime()), pattern);
    }

    /**
     * 获取指定时间是周几
     * @param time
     * @return
     */
    public static int week(LocalDateTime time) {
        return time.getDayOfWeek().getValue();
    }

    /**
     * 获取加或减N月的第一天
     * @param num
     * @return
     */
    public static LocalDateTime monthFirst(int num) {
        LocalDateTime newTime = plus(LocalDateTime.now(), num, ChronoUnit.MONTHS);
        newTime = newTime.with(TemporalAdjusters.firstDayOfMonth());
        return getDayStart(newTime);
    }

    /**
     * 获取加或减N月的最后天
     * @param num
     * @return
     */
    public static LocalDateTime monthLast(int num) {
        LocalDateTime newTime = plus(LocalDateTime.now(), num, ChronoUnit.MONTHS);
        newTime = newTime.with(TemporalAdjusters.lastDayOfMonth());
        return getDayEnd(newTime);
    }

    /**
     * 获取加或减N周的第一天
     * @param num
     * @return
     */
    public static LocalDateTime weekFirst(int num) {
        int week = week(LocalDateTime.now());
        LocalDateTime newTime = subtract(LocalDateTime.now(), week - 1, ChronoUnit.DAYS);
        newTime = plus(newTime, num * 7, ChronoUnit.DAYS);
        //formatTime(, "yyyy-MM-dd HH:mm:ss:SSS");
        return getDayStart(newTime);
    }

    /**
     * 获取加或减N周的最后一天
     * @param num
     * @return
     */
    public static LocalDateTime weekLast(int num) {
        int week = week(LocalDateTime.now());
        LocalDateTime newTime = plus(LocalDateTime.now(), 7 - week, ChronoUnit.DAYS);
        newTime = plus(newTime, num * 7, ChronoUnit.DAYS);
        return getDayEnd(newTime);
    }

    /**
     * 判断时间 ==>  t1 < t2 = true （2019-10-13 11:11:00 < 2020-11-13 13:13:00 = true）
     * @param t1
     * @param t2
     * @return
     */
    public static boolean isBefore(LocalDateTime t1, LocalDateTime t2) {
        return t1.isBefore(t2);
    }

    /**
     * 判断时间 ==>  t1 > t2 = true（2019-10-13 11:11:00 > 2020-11-13 13:13:00 = false）
     * @param t1
     * @param t2
     * @return
     */
    public static boolean isAfter(LocalDateTime t1, LocalDateTime t2) {
        return t1.isAfter(t2);
    }

    /**
     * 自构建 LocalDateTime ==> 年，月，日，时，分
     * @param year
     * @param month
     * @param dayOfMonth
     * @param hour
     * @param minute
     * @return
     */
    public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute) {
        return LocalDateTime.of(year, month, dayOfMonth, hour, minute);
    }

    /**
     * 自构建 LocalDateTime ==> 年，月，日，时，分，秒，毫秒（精确到9位数）
     * @param year
     * @param month
     * @param dayOfMonth
     * @param hour
     * @param minute
     * @param second
     * @param nanoOfSecond
     * @return
     */
    public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond) {
        return LocalDateTime.of(year, month, dayOfMonth, hour, minute, second, nanoOfSecond);
    }

    /**
     * Date 转 LocalDateTime
     * @param date
     * @return
     */
    public static LocalDateTime convertDateToLDT(Date date) {
        return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
    }

    /**
     * LocalDateTime 转 Date
     * @param time
     * @return
     */
    public static Date convertLDTToDate(LocalDateTime time) {
        return Date.from(time.atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * 获取指定日期的毫秒
     * @param time
     * @return
     */
    public static Long getMilliByTime(LocalDateTime time) {
        return time.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    /**
     * 获取指定日期的秒
     * @param time
     * @return
     */
    public static Long getSecondsByTime(LocalDateTime time) {
        return time.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();
    }

    /**
     * 获取指定时间的指定格式 ==> yyyy-MM-dd HH:mm:ss:SSS  (HH是24小时制，而hh是12小时制, ss是秒，SSS是毫秒)
     * @param time
     * @param pattern
     * @return
     */
    public static String formatTime(LocalDateTime time, String pattern) {
        return time.format(DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * 日期加上一个数,根据field不同加不同值,field为ChronoUnit.*
     * @param time
     * @param number
     * @param field
     * @return
     */
    public static LocalDateTime plus(LocalDateTime time, long number, TemporalUnit field) {
        return time.plus(number, field);
    }

    /**
     * 日期减去一个数,根据field不同减不同值,field参数为ChronoUnit.*
     * @param time
     * @param number
     * @param field
     * @return
     */
    public static LocalDateTime subtract(LocalDateTime time, long number, TemporalUnit field) {
        return time.minus(number, field);
    }

    /**
     * 获取两个日期的差，field参数为ChronoUnit.*
     * @param startTime
     * @param endTime
     * @param field     单位(年月日时分秒)
     **/
    public static long betweenTwoTime(LocalDateTime startTime, LocalDateTime endTime, ChronoUnit field) {
        Period period = Period.between(LocalDate.from(startTime), LocalDate.from(endTime));
        if (field == ChronoUnit.YEARS) {
            return period.getYears();
        }
        if (field == ChronoUnit.MONTHS) {
            return period.getYears() * 12 + period.getMonths();
        }
        return field.between(startTime, endTime);
    }

    /**
     * 获取指定某一天的开始时间 00:00:00
     * @param time
     * @return
     */
    public static LocalDateTime getDayStart(LocalDateTime time) {
        return time.withHour(0).withMinute(0).withSecond(0).withNano(0);
    }


    /**
     * 获取指定某一天的结束时间  23:59:59.999
     * @param time
     * @return
     */
    public static LocalDateTime getDayEnd(LocalDateTime time) {
        return time.withHour(23).withMinute(59).withSecond(59).withNano(999999999);
    }

    /**
     * 获取指定某月一天的结束时间  23:59:59.999
     * @param time
     * @param dayOfMonth
     * @param dayOfYear
     * @return
     */
    public static LocalDateTime getMonthDayEnd(LocalDateTime time,int dayOfMonth,int dayOfYear) {
        return time.withDayOfMonth(dayOfMonth).withDayOfYear(dayOfYear).withHour(23).withMinute(59).withSecond(59).withNano(999999999);
    }

    /**
     * 获取本周周一
     * @param time
     * @return
     */
    public static LocalDateTime getWeekOfFirst(LocalDateTime time){
        return time.with(TemporalAdjusters.previous(DayOfWeek.SUNDAY)).plusDays(1).withHour(0).withMinute(0).withSecond(0);
    }

    /**
     * 获取本周周日
     * @param time
     * @return
     */
    public static LocalDateTime getWeekOfLast(LocalDateTime time){
        return time.with(TemporalAdjusters.next(DayOfWeek.MONDAY)).minusDays(1).withHour(23).withMinute(59).withSecond(59);
    }

    /**
     * 获取本月第一天
     * @param time
     * @return
     */
    public static LocalDateTime getMonthOfFirst(LocalDateTime time){
        LocalDateTime firstday = time.with(TemporalAdjusters.firstDayOfMonth());
        return LocalDateTime.of(firstday.toLocalDate(), LocalTime.MIN);
    }

    /**
     * 获取本月最后一天
     * @param time
     * @return
     */
    public static LocalDateTime getMonthOfLast(LocalDateTime time){
        LocalDateTime lastDay = time.with(TemporalAdjusters.lastDayOfMonth());
        return LocalDateTime.of(lastDay.toLocalDate(),LocalTime.MAX);
    }

    /**
     * 获取当前时间月到 +N月的所有时间，一天一条数据 List<DateDays>
     * @param num
     * @return
     */
    public static List<DateDays> getDateDaysUpList(Integer num) {
        LocalDateTime startTime = monthFirst(0);        // 本月第一天  00:00:00
        LocalDateTime entTime = monthLast(num);              // 两月后的最后一天 23:59:59.999
        // 3个月的数据
        List<DateDays> everyDays = new ArrayList<>();
        // 第一天数据
        everyDays.add(new DateDays(startTime, week(startTime)));
        while (true) {
            // 获取一天后时间
            LocalDateTime nextDay = plus(everyDays.get(everyDays.size() - 1).getStartTime(), 1, ChronoUnit.DAYS);
            //大于两月后最后一天-跳出循环
            if (isAfter(nextDay, entTime)) {
                break;
            }
            everyDays.add(new DateDays(nextDay, week(nextDay)));
        }
        return everyDays;
    }

    /**
     *  获取当前时间开始，结束时间
     */
    public static long getCurrentDate(){
        //当天开始时间
        LocalDateTime todayStart = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        //当天结束时间
        LocalDateTime todayEnd = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        long startTime = LocalDateTimeUtil.toEpochMilli(todayStart);
        long endTime = LocalDateTimeUtil.toEpochMilli(todayEnd);

        //本月第一天
        LocalDateTime firstDay = LocalDateTime.now().with(TemporalAdjusters.firstDayOfMonth()).withHour(0).withMinute(0).withSecond(0);
        //本月最后一天
        LocalDateTime lastDay = LocalDateTime.now().with(TemporalAdjusters.lastDayOfMonth()).withHour(23).withMinute(59).withSecond(59);
        long startTimes = LocalDateTimeUtil.toEpochMilli(firstDay);
        long endTimes = LocalDateTimeUtil.toEpochMilli(lastDay);
        return startTime+endTime+startTimes+endTimes;

    }

    /**
     * 当前时间
     * @return
     */
    public static Date getCurrentData(){
        return new Date();
    }

    /**
     * localDate转date
     * @param localDate
     * @return
     */
    public static Date asDateByLocalDate(LocalDate localDate) {
        return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * localDateTime转date
     * @param localDateTime
     * @return
     */
    public static Date asDateByLocalDateTime(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * date转localDate
     * @param date
     * @return
     */
    public static LocalDate asLocalDate(Date date) {
        return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
    }

    /**
     * date转localDateTime
     * @param date
     * @return
     */
    public static LocalDateTime asLocalDateTime(Date date) {
        return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

    /**
     * 获取本日开始时间
     * @return 开始时间
     */
    public static String getStartTime(){
        LocalDateTime startTime = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        return  startTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * 获取本日结束时间
     * @return
     */
    public static String getEndTime(){
        LocalDateTime endTime = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        return endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * 获取本周开始时间
     * @return
     */
    public static String getStartWeek(){
        LocalDate today = LocalDate.now();
        LocalDate oneDayOfWeek = getOneDayOfWeek(today, 1);
        return LocalDateTime.of(oneDayOfWeek,LocalTime.MIN).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * 获取本周的结束时间
     * @return
     */
    public static String getEndWeek(){
        LocalDate today = LocalDate.now();
        LocalDate oneDayOfWeek = getOneDayOfWeek(today, 7);
        return LocalDateTime.of(oneDayOfWeek,LocalTime.MAX).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * 获取一周内的某一天
     * @param today 这周内任意一天的日期
     * @param day 想要获取一周中的第几天
     * @return LocalDate
     */
    private static LocalDate getOneDayOfWeek(TemporalAccessor today, int day){
        TemporalField fieldIso = WeekFields.of(DayOfWeek.MONDAY, 1).dayOfWeek();
        LocalDate localDate = LocalDate.from(today);
        return localDate.with(fieldIso, day);
    }

    /**
     * 获取本月的开始日期
     * @return
     */
    public static String getOneDayOfMonth(){
        LocalDate date = LocalDate.now();
        LocalDate with = date.with(TemporalAdjusters.firstDayOfMonth());
        return LocalDateTime.of(with,LocalTime.MIN).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * 获取本月的结束日期
     * @return
     */
    public static String getEndDayOfMonth(){
        LocalDate date = LocalDate.now();
        LocalDate with = date.with(TemporalAdjusters.lastDayOfMonth());
        return LocalDateTime.of(with,LocalTime.MAX).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * 获取指定某月的开始日期
     * @param month 月份
     * @return
     */
    public static String getMonthStartOneDay(int month){
        LocalDate localDate = LocalDate.now();
        return LocalDateTime.of(LocalDate.of(localDate.getYear(),month,1),LocalTime.MIN).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * 获取指定月的结束日期
     * @param month 月份
     * @return
     */
    public static String getMonthEndTime(int month){
        YearMonth yearMonth = YearMonth.of(LocalDate.now().getYear(), month);
        LocalDate localDate = yearMonth.atEndOfMonth();
        return LocalDateTime.of(localDate,LocalTime.MAX).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * 获取本年度开始日期
     * @return
     */
    public static String getOneDayOfYear(){
        LocalDate date = LocalDate.now();
        LocalDate with = date.with(TemporalAdjusters.firstDayOfYear());
        return LocalDateTime.of(with,LocalTime.MIN).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * 获取本年度结束日期
     * @return
     */
    public static String getEndDayOfYear(){
        LocalDate date = LocalDate.now();
        LocalDate with = date.with(TemporalAdjusters.lastDayOfYear());
        return LocalDateTime.of(with,LocalTime.MAX).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * 根据开始时间结束时间获取时间段内所有时间
     * @param startTime
     * @param endTime
     * @return
     */
    public static List<String> getDateListOfDateRange(String startTime,String endTime){
        List<String> dateList = new ArrayList<>();
        long betweenDay = DateUtil.betweenDay(DateUtil.parse(startTime, "yyyy-MM-dd HH:mm:ss"), DateUtil.parse(endTime, "yyyy-MM-dd HH:mm:ss"), true);
        for (int i = 0; i <= betweenDay; i++) {
            DateTime dateTime = DateUtil.offsetDay(DateUtil.parse(startTime), i);
            dateList.add(DateUtil.format(dateTime, "yyyy-MM-dd"));
        }
        return dateList;
    }

    /**
     * 当前时间
     * @return
     */
    public static String getNow(){
        String newTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return newTime;
    }

    /**
     * 获取当前天的结束时间
     */
    public static Date getEndTime(Date date) {
        Calendar day = Calendar.getInstance();
        day.setTime(date);
        day.set(Calendar.HOUR_OF_DAY, 23);
        day.set(Calendar.MINUTE, 59);
        day.set(Calendar.SECOND, 59);
        day.set(Calendar.MILLISECOND, 999);
        return day.getTime();
    }

    /**
     * 时间距离当天结束时间的秒
     *
     * @param nowDate
     * @return
     */
    public static long getDayEndSecond(Date nowDate) {
        // 获得两个时间的毫秒时间差异
        long diff = DateUtils.getEndTime(new Date()).getTime() - nowDate.getTime();
        //计算两个时间之间差了多少秒
        long second = diff / 1000;
        return second;
    }


    /**
     * Calendar获取当天剩余的秒
     * @param currentDate
     * @return
     */
    public static Integer getRemainSecondsOneDay(Date currentDate) {
        Calendar midnight=Calendar.getInstance();
        midnight.setTime(currentDate);
        midnight.add(midnight.DAY_OF_MONTH,1);//将日加1
        midnight.set(midnight.HOUR_OF_DAY,0);//控制时
        midnight.set(midnight.MINUTE,0);//控制分
        midnight.set(midnight.SECOND,0);//控制秒
        midnight.set(midnight.MILLISECOND,0);//毫秒
        //通过以上操作就得到了一个currentDate明天的0时0分0秒的Calendar对象，然后相减即可得到到明天0时0点0分0秒的时间差
        Integer seconds=(int)((midnight.getTime().getTime()-currentDate.getTime())/1000);
        return seconds;
    }

    /**
     * LocalDateTime获取当天剩余的秒
     * @param currentDate
     * @return
     */
    public static Integer getRemainSecondsOneDays(Date currentDate) {
        //使用plusDays加传入的时间加1天，将时分秒设置成0
        LocalDateTime midnight = LocalDateTime.ofInstant(currentDate.toInstant(),
                ZoneId.systemDefault()).plusDays(1).withHour(0).withMinute(0)
                .withSecond(0).withNano(0);
        LocalDateTime currentDateTime = LocalDateTime.ofInstant(currentDate.toInstant(),
                ZoneId.systemDefault());
        //使用ChronoUnit.SECONDS.between方法，传入两个LocalDateTime对象即可得到相差的秒数
        long seconds = ChronoUnit.SECONDS.between(currentDateTime, midnight);
        return (int) seconds;
    }

    private static final DateTimeFormatter DEFAULT_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    /**
     * 根据Object类型获取日期格式
     * jdk1.8时间格式
     */
    private LocalDateTime getDateTime(Object object) {
        if (object instanceof LocalDateTime) {
            return (LocalDateTime) object;
        } else if (object instanceof Long) {
            return Instant.ofEpochMilli(Long.parseLong(object.toString())).atZone(ZoneId.systemDefault())
                    .toLocalDateTime();
        } else if (object instanceof String) {
            return LocalDateTime.parse(object.toString(), DEFAULT_FORMAT);
        }
        return null;
    }

    /**
     * 根据Object类型获取时间戳
     * jdk1.8时间格式
     */
    private Long getTimestamp(Object object) {
        if (object instanceof Long) {
            return Long.parseLong(object.toString());
        } else if (object instanceof String) {
            return LocalDateTime.parse(object.toString(), DEFAULT_FORMAT).atZone(ZoneId.systemDefault())
                    .toEpochSecond();
        } else if (object instanceof LocalDateTime) {
            return ((LocalDateTime) object).atZone(ZoneId.systemDefault()).toEpochSecond();
        }
        return null;
    }

    /**
     * 当天剩余毫秒,秒
     * @return
     */
    public static long getDiffSec(){
        //LocalDateTime和ChronoUnit为1.8新增
        LocalDateTime midnight = LocalDateTime.now().plusDays(1).withHour(0).withMinute(0).withSecond(0).withNano(0);
        long millSeconds = ChronoUnit.MILLIS.between(LocalDateTime.now(),midnight);
        long seconds = ChronoUnit.SECONDS.between(LocalDateTime.now(), midnight);
        System.out.println("当天剩余毫秒：" + millSeconds);
        System.out.println("当天剩余秒：" + seconds);
        return seconds;
    }

//    redisTemplate.opsForHash().increment(keyPrefix,"efwew",1);
//    //只统计当天的
//    LocalDateTime expireTime = LocalDateTime.now().withHour(23).withMinute(59).withSecond(59);
//    redisTemplate.expireAt(keyPrefix,Date.from(expireTime.atZone(ZoneId.systemDefault()).toInstant()));



    /**
     * 获取昨天日期
      * @return
     */
    public static String beforeDay(){
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH)+1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        String dateStr = "";
        int dayval = 0;
        if(day!=1){
            dayval = day + (-1);
            dateStr = String.valueOf(year)+"-"+String.format("%02d",month)+"-"+String.format("%02d",dayval);
        }else{
            month = month + (-1);
            dayval = getMayDay(year,month);
            dateStr = String.valueOf(year)+"-"+String.format("%02d",month)+"-"+String.format("%02d",dayval);
        }
        return dateStr;
    }

    /**
     * 当前年月最大天数
     * @param year
     * @param month
     * @return
     */
    public static int getMayDay(int year,int month){
        int days = -1;
        switch (month){
            case 4:
            case 6:
            case 9:
            case 11:
                days = 30;
                break;
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                days = 31;
                break;
            case 2:
                if(isLeapYear(year)){
                    days = 29;
                }else{
                    days = 28;
                }
            default:
                System.out.println("输入年月份不合法");
                break;
        }
        return days;
    }

    /**
     * 是否是瑞年
     * @param year
     * @return
     */
    public static Boolean isLeapYear(int year){
        if((year%4==0 && year%100!=0) || year%400==0){
            return true;
        }
        return false;
    }

    /**
     * 获取YYYY格式
     *
     * @return
     */
    public static String getYear() {
        return formatDate(new Date(), "yyyy");
    }

    /**
     * 获取当前月份
     *
     * @return
     */
    public static String getMonth() {
        return formatDate(new Date(), "MM");
    }

    /**
     * 获取YYYY格式
     *
     * @return
     */
    public static String getYear(Date date) {
        return formatDate(date, "yyyy");
    }

    /**
     * 获取YYYY-MM-DD格式
     *
     * @return
     */
    public static String getDay() {
        return formatDate(new Date(), "yyyy-MM-dd");
    }

    /**
     * 获取YYYY-MM-DD格式
     *
     * @return
     */
    public static String getDay(Date date) {
        return formatDate(date, "yyyy-MM-dd");
    }

    /**
     * 获取YYYYMMDD格式
     *
     * @return
     */
    public static String getDays() {
        return formatDate(new Date(), "yyyyMMdd");
    }

    /**
     * 获取YYYYMMDD格式
     *
     * @return
     */
    public static String getDays(Date date) {
        return formatDate(date, "yyyyMMdd");
    }

    /**
     * 获取YYYY-MM-DD HH:mm:ss格式
     *
     * @return
     */
    public static String getTime() {
        return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 获取YYYY-MM-DD HH:mm:ss.SSS格式
     *
     * @return
     */
    public static String getMsTime() {
        return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS");
    }

    /**
     * 获取YYYYMMDDHHmmss格式
     *
     * @return
     */
    public static String getAllTime() {
        return formatDate(new Date(), "yyyyMMddHHmmss");
    }


    /**
     * 获取YYYYMMDDHHmmssSSS格式
     *
     * @return
     */
    public static String getAllMSTime() {
        return formatDate(new Date(), "yyyyMMddHHmmssSSS");
    }

    /**
     * 获取YYYY-MM-DD HH:mm:ss格式
     *
     * @return
     */
    public static String getTime(Date date) {
        return formatDate(date, "yyyy-MM-dd HH:mm:ss");
    }

    public static String formatDate(Date date, String pattern) {
        String formatDate = null;
        if (StringUtils.isNotBlank(pattern)) {
            formatDate = DateFormatUtils.format(date, pattern);
        } else {
            formatDate = DateFormatUtils.format(date, "yyyy-MM-dd");
        }
        return formatDate;
    }

    /**
     * compareDate
     * (日期比较，如果s>=e 返回true 否则返回false)
     */
    public static boolean compareDate(String s, String e) {
        if (parseDate(s) == null || parseDate(e) == null) {
            return false;
        }
        return parseDate(s).getTime() >= parseDate(e).getTime();
    }

    /**
     * 格式化日期
     *
     * @return
     */
    public static Date parseDatePattern(String date, String pattern) {
        return parse(date, pattern);
    }

    /**
     * 格式化日期
     *
     * @return
     */
    public static Date parseDate(String date) {
        return parse(date, "yyyy-MM-dd");
    }

    /**
     * 格式化日期
     *
     * @return
     */
    public static Date parseTime(String date) {
        return parse(date, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 格式化日期
     *
     * @return
     */
    public static Date parse(String date, String pattern) {
        try {
            return DateUtils.parseDatePattern(date, pattern);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 格式化日期
     *
     * @return
     */
    public static String format(Date date, String pattern) {
        return DateFormatUtils.format(date, pattern);
    }

    /**
     * 把日期转换为Timestamp
     *
     * @param date
     * @return
     */
    public static Timestamp format(Date date) {
        return new Timestamp(date.getTime());
    }

    /**
     * 校验日期是否合法
     *
     * @return
     */
    public static boolean isValidDate(String s) {
        return parse(s, "yyyy-MM-dd HH:mm:ss") != null;
    }

    /**
     * 校验日期是否合法
     *
     * @return
     */
    public static boolean isValidDate(String s, String pattern) {
        return parse(s, pattern) != null;
    }

    public static int getDiffYear(String startTime, String endTime) {
        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        try {
            int years = (int) (((fmt.parse(endTime).getTime() - fmt.parse(
                    startTime).getTime()) / (1000 * 60 * 60 * 24)) / 365);
            return years;
        } catch (Exception e) {
            // 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
            return 0;
        }
    }

    /**
     * <li>功能描述：时间相减得到天数
     */
    public static long getDaySub(String beginDateStr, String endDateStr) {
        long day = 0;
        SimpleDateFormat format = new SimpleDateFormat(
                "yyyy-MM-dd");
        Date beginDate = null;
        Date endDate = null;
        try {
            beginDate = format.parse(beginDateStr);
            endDate = format.parse(endDateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        day = (endDate.getTime() - beginDate.getTime()) / (24 * 60 * 60 * 1000);
        System.out.println("相隔的天数="+day);
        return day;
    }

    /**
     * 两个时间之间的天数
     *
     * @param beginTime
     * @param endTime
     * @return
     */
    public static Long getDaySub(Date beginTime, Date endTime) {
        return (endTime.getTime() - beginTime.getTime()) / (24 * 60 * 60 * 1000);
    }

    /**
     * 得到n天之后的日期
     * @param days
     * @return
     */
    public static String getAfterDayDate(String days) {
        int daysInt = Integer.parseInt(days);
        Calendar calendar = Calendar.getInstance(); // java.util包
        calendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动
        Date date = calendar.getTime();

        SimpleDateFormat sdfd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = sdfd.format(date);
        return dateStr;
    }

    /**
     * 得到n天之后是周几
     * @param days
     * @return
     */
    public static String getAfterDayWeek(String days) {
        int daysInt = Integer.parseInt(days);
        Calendar calendar = Calendar.getInstance();
        // 日期减 如果不够减会将月变动
        calendar.add(Calendar.DATE, daysInt);
        Date date = calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("E");
        String dateStr = sdf.format(date);
        return dateStr;
    }

//    /**
//     * 格式化Oracle Date
//     * @param value
//     * @return
//     */
//	public static String buildDateValue(Object value){
//		if(Func.isOracle()){
//			return "to_date('"+ value +"','yyyy-mm-dd HH24:MI:SS')";
//		}else{
//			return Func.toStr(value);
//		}
//	}

    /**
     * 时间戳转换成日期格式字符串
     * @param seconds 精确到秒的字符串
     * @param format
     * @return
     */
    public static String time2String(String seconds, String format) {
        if (seconds == null || seconds.isEmpty() || "null".equals(seconds)) {
            return "";
        }
        if (format == null || format.isEmpty()) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.parseLong(seconds + "000")));
    }

    /**
     * 日期格式字符串转换成时间戳
     * @param date_str 字符串日期
     * @param format   如：yyyy-MM-dd HH:mm:ss
     * @return long
     */
    public static Long str2TimeStamp(String date_str, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return Long.valueOf(sdf.parse(date_str).getTime() / 1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 日期格式字符串转换成时间戳
     * @param date_str 字符串日期
     * @param format   如：yyyy-MM-dd HH:mm:ss
     * @return long
     */
    public static Long str2TimeStampMillions(String date_str, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.parse(date_str).getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获得该月第一天
     *
     * @param year
     * @param month
     * @return
     */
    public static String getFirstDayOfMonth(int year, int month) {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR, year);
        //设置月份
        cal.set(Calendar.MONTH, month - 1);
        //获取某月最小天数
        int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最小天数
        cal.set(Calendar.DAY_OF_MONTH, firstDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String firstDayOfMonth = sdf.format(cal.getTime());
        return firstDayOfMonth;
    }

    /**
     * 获得该月最后一天,实际获得下月第一天
     * @param year
     * @param month
     * @return
     */
    public static String getLastDayOfMonth(int year, int month) {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR, year);
        //设置月份
        cal.set(Calendar.MONTH, month);
        //获取某月最小天数
        int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最小天数
        cal.set(Calendar.DAY_OF_MONTH, firstDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String firstDayOfMonth = sdf.format(cal.getTime());
        return firstDayOfMonth;
    }

    /**
     * 当前月第一天
     * @param pattern
     * @return
     */
    public static String getCurrentLastDayOfMonth(String pattern) {
        Calendar cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 1);
        cale.set(Calendar.DAY_OF_MONTH, 0);
        if (pattern == null || pattern.isEmpty()) {
            pattern = "yyyy-MM-dd";
        }
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        String firstday = format.format(cale.getTime());
        return firstday;
    }

    /**
     * 获取当前月第一天
     * @param pattern
     * @return
     */
    public static String getCurrentFirstDayOfMonth(String pattern) {
        Calendar cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        if (pattern == null || pattern.isEmpty()) {
            pattern = "yyyy-MM-dd";
        }
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        String firstday = format.format(cale.getTime());
        return firstday;
    }

    /**
     * 获取下个月第一天
     * @param pattern
     * @return
     */
    public static String getNextMonthFirstDay(String pattern) {
        Calendar cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 1);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        if (pattern == null || pattern.isEmpty()) {
            pattern = "yyyy-MM-dd";
        }
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        String firstday = format.format(cale.getTime());
        return firstday;
    }

    /**
     * 每周的第一天和最后一天
     * @param dataStr
     * @param dateFormat
     * @param resultDateFormat
     * @return
     * @throws ParseException
     */
    public static String getFirstOfWeek(String dataStr, String dateFormat, String resultDateFormat) {
        Calendar cal = Calendar.getInstance();
        try {
            cal.setTime(new SimpleDateFormat(dateFormat).parse(dataStr));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int d = 0;
        if (cal.get(Calendar.DAY_OF_WEEK) == 1) {
            d = -6;
        } else {
            d = 2 - cal.get(Calendar.DAY_OF_WEEK);
        }
        cal.add(Calendar.DAY_OF_WEEK, d);
        // 所在周开始日期
        String data1 = new SimpleDateFormat(resultDateFormat).format(cal.getTime());
        return data1;

    }

    /**
     * 每周的最后一天
     * @param dataStr
     * @param dateFormat
     * @param resultDateFormat
     * @throws ParseException
     */
    public static String getLastOfWeek(String dataStr, String dateFormat, String resultDateFormat) {
        Calendar cal = Calendar.getInstance();
        try {
            cal.setTime(new SimpleDateFormat(dateFormat).parse(dataStr));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int d = 0;
        if (cal.get(Calendar.DAY_OF_WEEK) == 1) {
            d = -6;
        } else {
            d = 2 - cal.get(Calendar.DAY_OF_WEEK);
        }
        cal.add(Calendar.DAY_OF_WEEK, d);
        cal.add(Calendar.DAY_OF_WEEK, 6);
        // 所在周结束日期
        String data2 = new SimpleDateFormat(resultDateFormat).format(cal.getTime());
        return data2;

    }

    //获取当天的开始时间
    public static Date getDayBegin() {
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    //获取当天的结束时间
    public static Date getDayEnd() {
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        return cal.getTime();
    }


    /**
     * 获取指定时间year年后的第二天凌晨
     * @param currentDate 指定date
     * @param year        年
     * @return
     */
    public static Date getDateAfterYear(Date currentDate, int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentDate);
        calendar.add(Calendar.YEAR, +year);
        calendar.add(Calendar.DATE, +1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获得指定时间后的day天后的第二天凌晨
     * @param currentDate 当前时间
     * @param day         天
     * @return
     */
    public static Date getDateAfterDays(Date currentDate, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentDate);
        calendar.add(Calendar.DATE, day+1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获得指定时间后的day天后的时间
     * @param currentDate 当前时间
     * @param day         天
     * @return
     */
    public static Date getDateAfterDay(Date currentDate, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentDate);
        calendar.add(Calendar.DATE, day);
        return calendar.getTime();
    }

    /**
     * 获取指定月所在的季度
     * @param month
     * @return
     */
    public static String getQuarter(String month) {
        String quarter = "";
        int m = Integer.parseInt(month);
        if (m >= 1 && m <= 3) {
            quarter = "1";
        }
        if (m >= 4 && m <= 6) {
            quarter = "2";
        }
        if (m >= 7 && m <= 9) {
            quarter = "3";
        }
        if (m >= 10 && m <= 12) {
            quarter = "4";
        }
        return quarter;
    }

    /**
     * 获取每隔季度的时间限制
     * @param year
     * @param quarter
     * @return
     */
    public static List<String> getSeasonTimeLimit(String year, String quarter) {
        List<String> timeLimit = new ArrayList<>();
        if ("1".equals(quarter)) {
            timeLimit.add(year.concat("-01-01 00:00:00"));
            timeLimit.add(year.concat("-03-31 23:59:59"));
        } else if ("2".equals(quarter)) {
            timeLimit.add(year.concat("-04-01 00:00:00"));
            timeLimit.add(year.concat("-06-30 23:59:59"));
        } else if ("3".equals(quarter)) {
            timeLimit.add(year.concat("-07-01 00:00:00"));
            timeLimit.add(year.concat("-09-30 23:59:59"));
        } else {
            timeLimit.add(year.concat("-10-01 00:00:00"));
            timeLimit.add(year.concat("-12-31 23:59:59"));
        }
        return timeLimit;
    }



}
